////////////////////////////////////////////////////
//
//  timer.c
//  implementation of system timer for OS simulation.
//
//  author: Phillip Cardon
//
////////////////////////////////////////////////////

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include "cpu.h"

// The timer which interrupts the cpu on a fixed schedule.
void *timer_start(void *the_cpu)
{
    CPUPtr temp_cpu = (CPUPtr)the_cpu;
    int timer_int = TIMER_INTERRUPT;
    printf("Timer has been activated.\n");
    while(1)
    {
        sleep(1);
        if (temp_cpu->run_var == EXIT_VAR)
        {
            pthread_exit(NULL);
        }
        pthread_mutex_lock(temp_cpu->interrupt_lock);
        queue_enqueue(temp_cpu->int_controller, &timer_int, QUEUE_MAX_PRI);
        printf("Timer Interrupt generated by timer.\n");
        pthread_mutex_unlock(temp_cpu->interrupt_lock);
    }
}
